import hashlib
import time
import requests


class ApiClient:
    def __init__(self, api_key, base_url):
        self.api_key = api_key
        self.base_url = base_url
        self.session = requests.session()

    def _generate_sign(self, data):
        sorted_data = sorted(data.items())
        sign_str =f"{self.api_key}{data['timestamp']}{data['uid']}{data['utoken']}indexnongchanggetgamedetail"
        print(sign_str)
        return hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    def get_game_detail(self):
        url ='https://app.szyqg.cloud/api/index.php?c=index&a=nongchang&do=getgamedetail'
        data = {
            'uid': '104031',
            'utoken': '8cc573319ea19d4e932b052b8382f843',
            'loading': '',
            'timestamp': int(time.time() * 1000) + 5000,
        }
        sign = self._generate_sign(data)
        data['Sign'] = sign
        headers = {'Authortoken': sign, 'Content-Type': 'application/x-www-form-urlencoded'}
        print(data)
        response = self.session.post(url, headers=headers, data=data)
        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(f'Error {response.status_code}: {response.text}')

if __name__ == '__main__':
    i_key = 'qugoushenghuo20230405..'
    base_url = 'https://app.szyqg.cloud'
    client = ApiClient(i_key, base_url)
    game_detail = client.get_game_detail()
    print(game_detail)